﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.Script.Serialization;
using Kaiser.Extensions;
 

namespace Kaiser.Models.JsonModels
{
   

    [Serializable]
    public class JsonTreeNode
    {
        public string id
        {
            get;
            set;
        }

        public string text
        {
            get;
            set;
        }

        public string state
        {
            get;
            set;
        }
        public List<JsonTreeNode> children
        {
            get;
            set;
        }
    }
    public class JsonTree
    {
        public readonly List<JsonTreeNode> RootList = new List<JsonTreeNode>() { };
        private readonly string _columnName = "";
        private readonly string _columnId = "";
        private readonly string _columnParentId = "";
        public JsonTree(DataTable dt, string columnName, string columnId, string columnparentId)
        {
            _columnId = columnId;
            _columnName = columnName;
            _columnParentId = columnparentId;
            TreeTable = dt;
        }

        public static  List<JsonTreeNode>  ConvertToJsonTree(DataTable dt, string columnName, string columnId, string columnparentId,object parentId)
        {

            DataRow[] rows = null;

            rows = dt.Select(columnparentId + "='{0}'".FormatWith(parentId));

        
           var list = new List<JsonTreeNode>();
            foreach (DataRow dr in rows)
            {
                var node = new JsonTreeNode();
                node.id = dr[columnId].ToString();
                node.text = dr[columnName].ToString();
                AddChildren(dt, columnName, columnId, columnparentId, node);
                list.Add(node);
            }
            return list;
        }


        public static void AddChildren(DataTable dt, string columnName, string columnId, string columnparentId, JsonTreeNode parentNode)
        {

            DataRow[] rows = null;

            rows = dt.Select(columnparentId + "='{0}'".FormatWith(parentNode.id));
            if (rows.Length<=0)
            {
                return; 
            }
        
            var list = new List<JsonTreeNode>();
            foreach (DataRow dr in rows)
            {
                var node = new JsonTreeNode();
                node.id = dr[columnId].ToString();
                node.text = dr[columnName].ToString();
                AddChildren(dt, columnName, columnId, columnparentId, node);
                //node.children.AddRange(ConvertToJsonTree());
                list.Add(node);
            }
            parentNode.children=new List<JsonTreeNode>();
            parentNode.children.AddRange(list);
            
        }

        [ScriptIgnore]
        private DataTable TreeTable
        {
            get;
            set;
        }

        public string ToJson()
        {
            BuildTree();
            var json = new JavaScriptSerializer();
            return json.Serialize(this.RootList);
        }
        private void BuildTree()
        {
            LoadTree(RootList[0], this.TreeTable, true);
        }

        private JsonTreeNode ConvertToJsonTreeNode(DataRow dr, DataTable dt)
        {
            var node = new JsonTreeNode();
            node.id = dr[_columnId].ToString();
            node.text = dr[_columnName].ToString();
            LoadTree(node, dt, false);
            return node;
        }

        private void LoadTree(string parentId, DataTable dt)
        {

            DataRow[] rows = null;

            rows = dt.Select(_columnParentId + "='{0}'".FormatWith(parentId));


            var list= new List<JsonTreeNode>();
            foreach (DataRow dr in rows)
            {
                var node = new JsonTreeNode();
                node.id = dr[_columnId].ToString();
                node.text = dr[_columnName].ToString();

                node.children.Add(ConvertToJsonTreeNode(dr, dt));
                list.Add(node);
            }

            //return JsonTree;
        }

        private void LoadTree(JsonTreeNode parentNode, DataTable dt, bool isfirst)
        {

            DataRow[] rows = null;

            rows = dt.Select(_columnParentId + "='{0}'".FormatWith(parentNode.id));


            parentNode.children = new List<JsonTreeNode>();
            foreach (DataRow dr in rows)
            {
                parentNode.children.Add(ConvertToJsonTreeNode(dr, dt));
            }

            //return JsonTree;
        }
    }
}
